package com.portal4kids.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;

import com.portal4kids.model.Info;
import com.portal4kids.util.HibernateUtil;

public class InfoManager extends HibernateUtil{
	
	public Info editInfo(Info info){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Info> infoList = new ArrayList<Info>();
		Query q = session.createQuery("from Info where type='" + info.getType() + "'");
		infoList = q.list();
		
		if(infoList.isEmpty()){
			session.save(info);
		}else{
			Info inf = (Info) session.load(Info.class, info.getId_info());
			inf.setDescription(info.getDescription());
			session.update(inf);
		}
		
		session.getTransaction().commit();
		return info;
		
	}
	
	public List<Info> getInfoFromDB(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Info> infoList = new ArrayList<Info>();
		Query q = session.createQuery("from Info");
		infoList = q.list();
		session.getTransaction().commit();
		return infoList;
		
	}
	
	public List<Info> getInfoFromDBAfterType(String type){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Info> infoList = new ArrayList<Info>();
		Query q = session.createQuery("from Info where type='" + type + "'");
		infoList = q.list();
		session.getTransaction().commit();
		return infoList;
		
	}

}
